using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Text;
using System.IO;
using System.Xml;
using System.Security.Cryptography;

namespace SIS.BLL.Entities
{
    public static class Common
    {
        public static bool IsNullOrDBNull(object obj)
        {
            return (obj == null || obj == DBNull.Value);
        }

        public static string ToString(object obj)
        {
            string rtn = string.Empty;
            if (obj != null)
            {
                rtn = obj.ToString();
            }
            return rtn;
        }
        public static string Key()
        {
            return "&%#@?,:*";
        }
        public static void FillDropDownList(DropDownList ddl, DataTable data, string value, string display)
        {
            if (ddl.DataSource != null)
            {
                ddl.DataSource = null;
            }

            ddl.DataSource = data;
            ddl.DataTextField = display;
            ddl.DataValueField = value;
            ddl.DataBind();
        }

        public static void FillDropDownList(DropDownList ddl, DataTable data, string value, string display, string defaultItem)
        {
            ddl.DataSource = data;
            ddl.DataTextField = display;
            ddl.DataValueField = value;
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem(defaultItem, "0"));
        }
        public static string Encrypt(string strText, string strEncrKey)
        {
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
            try
            {
                //Dim bykey() As Byte = System.Text.Encoding.UTF8.GetBytes(Left(strEncrKey, 8));
                ///textBox3.Text = Decrypt(textBox2.Text, "&%#@?,:*");


                byte[] bykey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8)); //Strings.Left(strEncrKey, 8));
                byte[] InputByteArray = System.Text.Encoding.UTF8.GetBytes(strText);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(bykey, IV), CryptoStreamMode.Write);
                cs.Write(InputByteArray, 0, InputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static string Decrypt(string strText, string sDecrKey)
        {
            byte[] IV = { 18, 52, 86, 120, 144, 171, 205, 239 };
            byte[] inputByteArray = new byte[strText.Length + 1];
            try
            {
                byte[] byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));// Strings.Left(sDecrKey, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(strText);
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static string GetRandomUserId()
        {
            //if (iLength < 1) iLength = 1;
            //if (iLength > 40) iLength = 40;
            String sSeed = Guid.NewGuid().ToString().Replace("-", "");
            return sSeed.Substring(0, 11);
        }
        public static string GetRandomPassword()
        {
            String sSeed = Guid.NewGuid().ToString().Replace("-", "");
            return sSeed.Substring(0, 11);
        }
    }
}
